package com.leadnews.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leadnews.mapper.NovelContentMapper;
import com.leadnews.pojo.Novel;
import com.leadnews.pojo.NovelContent;
import com.leadnews.service.NovelService;
import com.leadnews.mapper.NovelMapper;
import com.leadnews.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 自己
* @description 针对表【novel】的数据库操作Service实现
* @createDate 2023-01-01 15:54:10
*/
@Service
@Slf4j
public class NovelServiceImpl extends ServiceImpl<NovelMapper, Novel>
    implements NovelService{

    @Autowired
    NovelMapper novelMapper;
    @Autowired
    NovelContentMapper novelContentMapper;
@Autowired
    RedisTemplate redisTemplate;
    @Override
    public ResponseResult getNovelName(String novelname) {
        ResponseResult result = new ResponseResult();
        LambdaQueryWrapper<Novel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Novel::getName, novelname);
        ArrayList<Object> list = new ArrayList<>();

        try {
            Novel novel = novelMapper.selectOne(queryWrapper);
            ListOperations listOperations = redisTemplate.opsForList();

            list.add(novel);
            List<String> mylist = listOperations.range(novel.getId()+"", 0, -1);
            for (String value : mylist) {
                list.add(value);
            }

            result.setCode(200);
            result.setData(list);
        } catch (Exception e) {
            log.info("未找到该小说");
            result.setCode(0);
        }
        return result;
    }

    @Override
    public ResponseResult getNovel() {
        return null;
    }
}




